
<template>
  <div class="bg">
    <div class="poab">
      <img src="~@/assets/list/logo.png"
           class="img-logo">
      <div class="kk">
        <div class="t1">企业提升数字化系统</div>
        <div class="t2">Enterprise upgrade digital system</div>
        <div class="bottom">
          <div class="matop-bott">
            <span class="title_style">账号登录</span>
          </div>
          <el-form :model="form"
                   :rules="rules"
                   v-show="!success"
                   class="ve_form"
                   ref="ref_form"
                   :inline="false"
                   @keyup.enter="onSubmit">
            <el-form-item prop="username">
              <el-input v-model.trim="form.username"
                        placeholder="用户名"><template #prepend><i style="font-size:20px;"
                     class="el-icon-user"></i></template></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model.trim="form.password"
                        show-password
                        placeholder="密码"><template #prepend><i style="font-size:20px;"
                     class="el-icon-key"></i></template></el-input>
            </el-form-item>
            <div class="matop"
                 @click="onSubmit">
              <span>登录</span>
            </div>
            <div class="pwtype">
              <span class="register"
                    @click="onRegister">注册</span>
              <span class="cell">/</span>
              <span class="forget"
                    @click="toPassword">忘记密码?</span>
            </div>
          </el-form>
        </div>
      </div>
      <div class="b-image">
        <img src="~@/assets/list/bg2.png"
             class="bg-img">
      </div>
    </div>
  </div>

</template>

<script>

import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'
import { postLogin } from "@/request/api";
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }]
};
const form = reactive({
  username: "",
  password: ""
});
export default {
  name: "Login",
  components: {},
  setup () {
    const store = useStore();
    const router = useRouter();

    const { username, password } = toRefs(form);
    const ref_form = ref(null);
    const success = ref(false);
    sessionStorage.clear();
    store.dispatch("app/set_token", "");
    router.options.isAddDynamicMenuRoutes = false;

    const onSubmit = () => {
      ref_form.value.validate(async valid => {
        if (valid) {
          postLogin(form).then(res => {
            if (res.data.code === 1) {
              store.dispatch("app/set_token", res.data.token.token);
              window.localStorage.setItem('token', res.data.token.token)
              window.localStorage.setItem('user', JSON.stringify(res.data.token))
              success.value = true;
              router.push({ name: "HomeIndex" });

            } else {
              ElMessage.error(res.data.msg)
            }
          })
        } else {
          return;
        }
      });
    };
    const onRegister = () => {
      router.push({ name: "Register" });
    };
    const toPassword = () => {
      router.push({ name: "Password" });
    };
    return {
      success,
      form,
      ref_form,
      rules,
      onSubmit,
      onRegister,
      toPassword
    };
  }
}

</script>

<style lang="scss" scoped>
.bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 1000px;
  z-index: -10;
  zoom: 1;
  background-color: #fff;
  background: url(../assets/list/background.png) no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
}
.matop-bott {
  text-align: center;
  margin-bottom: 20px;
  .title_style {
    font-size: 24px;
    font-family: ZhenyanGB;
    font-weight: 400;
    color: #6c91ed;
    font-weight: bold;
  }
}
.bg-img {
  width: 90%;
  height: 630px;
  margin: auto;
  display: block;
  padding-top: 150px;
}
.poab {
  position: relative;
  .img-logo {
    position: absolute;
    top: 23%;
    left: 12%;
    width: 99px;
    height: 64px;
  }
}
.kk {
  position: absolute;
  right: 12%;
  top: 17%;
  width: 25%;
}
.bottom {
  background: white;
  padding: 40px;
  box-shadow: 2px 3px 40px 0px rgb(44 61 94 / 13%);
  border-radius: 15px;
}
.img-title {
  width: 100px;
  height: 20px;
}
.matop-bott {
  text-align: center;
  margin-bottom: 30px;
}
.matop {
  margin-top: 70px;
  text-align: center;
  background: #6c91ed;
  color: white;
  padding-top: 10px;
  padding-bottom: 10px;
  border-radius: 5px;
  cursor: pointer;
}
.matop:active {
  background: #9ca5bb;
}
.pwtype {
  margin-top: 20px;
  text-align: right;
  .register {
    color: #6c91ed;
    cursor: pointer;
  }
  .cell {
    margin-left: 10px;
    color: #939499;
  }
  .forget {
    color: #939499;
    cursor: pointer;
  }
}
.t1 {
  color: #457af7;
  font-weight: 600;
  margin-bottom: 10px;
  font-size: 30px;
  margin-top: 60px;
}

.t2 {
  color: #7a8499;
  font-size: 13px;
  margin-bottom: 50px;
}
</style>
